package com.gears_software.sxdc.dccx.data.db;

import android.transition.PathMotion;

import com.gears_software.sxdc.dccx.data.model.LocLocationModel;
import com.gears_software.sxdc.dccx.data.model.NoticeModel;
import com.gears_software.sxdc.dccx.data.model.PathRecord;
import com.github.mzule.activityrouter.router.Path;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.query.In;
import com.j256.ormlite.support.DatabaseConnection;
import com.orhanobut.logger.Logger;

import org.androidannotations.annotations.EBean;
import org.androidannotations.ormlite.annotations.OrmLiteDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chezhenzhen on 2017/9/21.
 */
@EBean
public class DBUtils {
    @OrmLiteDao(helper = DataBaseHelper.class)
    Dao<NoticeModel, Integer> noticeDao;
    @OrmLiteDao(helper = DataBaseHelper.class)
    Dao<LocLocationModel, Integer> locationDao;

    public boolean insertNotice(NoticeModel model) {
        try {
            noticeDao.createOrUpdate(model);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public List<NoticeModel> getNoticeList(String userId) {
        List<NoticeModel> list = new ArrayList<NoticeModel>();
        QueryBuilder<NoticeModel, Integer> noticeQuery = noticeDao.queryBuilder();
        try {
            list = noticeQuery.orderBy("id", false).where().eq("userId",userId).query();
        } catch (SQLException e) {
            e.printStackTrace();
            Logger.d("获取失败");
        }
        return list;
    }

    public boolean deleteNoticeById(int id){
        DeleteBuilder<NoticeModel, Integer> noticeDelete = noticeDao.deleteBuilder();
        try {
            noticeDelete.where().eq("id", id);
            noticeDelete.delete();
        } catch (SQLException e) {
            e.printStackTrace();
            Logger.d("agent", "系统通知删除失败");
        }
        return true;
    }

    public boolean removeNoticeList(String userId) {
        DeleteBuilder<NoticeModel, Integer> noticeDelete = noticeDao.deleteBuilder();
        try {
            noticeDelete.where().eq("userId", userId);
            noticeDelete.delete();
        } catch (SQLException e) {
            e.printStackTrace();
            Logger.d("agent", "系统通知删除失败");
        }
        return true;
    }

    public boolean insertLocation(LocLocationModel locLocationModel) {
        try {

            locationDao.createOrUpdate(locLocationModel);
            Logger.d("保存成功");
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public List<LocLocationModel> getLocationList(String orderid) {
        List<LocLocationModel> list = new ArrayList<LocLocationModel>();
        QueryBuilder<LocLocationModel, Integer> locationQuery = locationDao.queryBuilder();
        try {
            list = locationQuery.orderBy("id", true).where().eq("orderid",orderid).query();
        } catch (SQLException e) {
            e.printStackTrace();
            Logger.d("获取失败");
        }
        return list;
    }
}
